# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: xguo

from coin.base.book import BookBuilderBase
from coin.exchange.base.kr_rest.currency import NativeCurrency
from coin.exchange.base.kr_rest.product import NativeProduct


class StubCurrency(NativeCurrency):
  _exchange = 'Stub'
  _currency_list = ['BTC', 'ETH', 'EOS', 'XRP', 'USDT']

  def __init__(self, currency):
    assert currency in type(self)._currency_list
    super().__init__(currency, currency)

  @classmethod
  def FromStrNativeCurrency(cls, currency, ts=None):
    assert currency in cls._currency_list
    return cls(currency)

  @classmethod
  def FromStrCurrency(cls, currency, ts=None):
    assert currency in cls._currency_list, currency
    return cls(currency)


class StubProduct(NativeProduct):
  _exchange = 'Stub'
  NativeCurrencyType = StubCurrency

  def __init__(self, base, quote):
    native_symbol = '%s-%s' % (base.native_currency, quote.native_currency)
    super().__init__(base, quote, native_symbol)

  @classmethod
  def FromStrNativeProduct(cls, product):
    return cls.FromStrProduct(product)


class BookEntry(object):
  def __init__(self, price, qty):
    assert price > 0
    assert qty > 0
    self.price = float(price)
    self.qty = float(qty)

  def __str__(self):
    return '(price=%s, qty=%s)' % (self.price, self.qty)


class BookBuilder(BookBuilderBase):
  ProductType = StubProduct

  def __init__(self, native_symbol):
    super().__init__("Stub", native_symbol, book_builder_observer=None)

  def snapshot(self, bids: list, asks: list, timestamp: int):
    self.update_timestamp(timestamp)
    self._ask_price_map.clear()
    self._bid_price_map.clear()

    for price, qty in bids:
      entry = BookEntry(price, qty)
      self._bid_price_map[price] = entry

    for price, qty in asks:
      entry = BookEntry(price, qty)
      self._ask_price_map[price] = entry
